package com.itc.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.BeanCopyUtils;
import com.itc.common.security.utils.SecurityUtils;
import com.itc.dispatch.domain.AvoidFeign;
import com.itc.dispatch.domain.UserConfig;
import com.itc.dispatch.mapper.AvoidFeignMapper;
import com.itc.dispatch.service.AvoidFeignService;
import com.itc.dispatch.service.UserConfigService;
import com.itc.dispatch.mapper.UserConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
* @author 智少
* @description 针对表【user_config】的数据库操作Service实现
* @createDate 2024-09-10 09:39:38
*/
@Service
public class UserConfigServiceImpl extends ServiceImpl<UserConfigMapper, UserConfig>
    implements UserConfigService{

    public final  Long userId = SecurityUtils.getUserId();

    @Autowired
    public AvoidFeignService avoidFeignService;

    @Override
    public R add(UserConfig userConfig) {
        LambdaQueryWrapper<UserConfig> userConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userConfigLambdaQueryWrapper.eq(UserConfig::getUserId,userId);
        userConfigLambdaQueryWrapper.eq(UserConfig::getPath,userConfig.getPath());
        Long aLong = this.baseMapper.selectCount(userConfigLambdaQueryWrapper);
        if (aLong > 0) {
            return R.fail("当前页面已有配置信息");
        }
        userConfig.setUserId(userId);
        userConfig.setCreateTime(LocalDate.now().toString());
        this.baseMapper.insert(userConfig);
        return R.ok("新增成功");
    }

    @Override
    public R getMsg(String path) {
        LambdaQueryWrapper<UserConfig> userConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userConfigLambdaQueryWrapper.eq(UserConfig::getUserId,userId);
        userConfigLambdaQueryWrapper.eq(UserConfig::getPath,path);
        userConfigLambdaQueryWrapper.last("limit 1");
        UserConfig userConfig = this.baseMapper.selectOne(userConfigLambdaQueryWrapper);
        return R.ok(userConfig);
    }

    @Override
    public R change(UserConfig userConfig) {
        LambdaQueryWrapper<UserConfig> userConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userConfigLambdaQueryWrapper.eq(UserConfig::getUserId,userId);
        userConfigLambdaQueryWrapper.eq(UserConfig::getPath,userConfig.getPath());
        this.baseMapper.update(userConfig,userConfigLambdaQueryWrapper);
        return R.ok("修改成功");
    }

    @Override
    public R delete(String path) {
        LambdaQueryWrapper<UserConfig> userConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userConfigLambdaQueryWrapper.eq(UserConfig::getUserId,userId);
        userConfigLambdaQueryWrapper.eq(UserConfig::getPath,path);
        this.baseMapper.delete(userConfigLambdaQueryWrapper);
        return R.ok("删除成功");
    }


}




